package power.game.sixtysix;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

import power.game.sixtysix.entity.Card;
import power.game.sixtysix.entity.GameState;
import power.game.sixtysix.logic.GamePlay;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GamePlay gp = new GamePlay();
		int lastWinner = 1;
		while (gp.getGameState().equals(GameState.Ongoing)) {
			int option;
			Card cardDown1 = null;
			Card cardDown2 = null;
			System.out.println("\n === NEW ROUND ===\n");
			if (lastWinner == 1) {
				System.out.println("<PLAYER 1>");
				System.out.println("Talon: " + gp.getTalon());
				option = choose(gp.getPlayer1Cards());
				if (option < 5) {
					cardDown1 = gp.player1PutCard(option);
				}
				System.out.println("Put:  " + cardDown1);
				
				System.out.println("\n<PLAYER 2>");
				System.out.println("Talon: " + gp.getTalon());
				System.out.println("Down:  " + cardDown1);
				option = choose(gp.getPlayer2Cards());
				if (option < 5) {
					cardDown2 = gp.player2PutCard(option);
				}
				System.out.println("Put:   " + cardDown2);
				System.out.println("Down:  " + cardDown1);
			} else {
				System.out.println("<PLAYER 2>");
				System.out.println("Talon: " + gp.getTalon());
				option = choose(gp.getPlayer2Cards());
				if (option < 5) {
					cardDown1 = gp.player2PutCard(option);
				}
				System.out.println("Put:  " + cardDown1);
				
				System.out.println("\n<PLAYER 1>");
				System.out.println("Talon: " + gp.getTalon());
				System.out.println("Down:  " + cardDown1);
				option = choose(gp.getPlayer1Cards());
				if (option < 5) {
					cardDown2 = gp.player1PutCard(option);
				}
				System.out.println("Put:   " + cardDown2);
				System.out.println("Down:  " + cardDown1);
			}
			System.out.println();
			lastWinner = gp.newRound();
			System.out.println("Winner of this round is: Player" + lastWinner);
		}
		
		System.out.println("The winner is : " + gp.getGameState());
	}

	private static int choose(Card[] cards) {
		System.out.println("\nChoose: ");
		List<Integer> validOptions = new LinkedList<Integer>();
		for (int i = 0; i < cards.length; i++) {
			System.out.println("["  + i + "] " + cards[i]);
			validOptions.add(i);
		}
		return readOption(validOptions);
	}
	
	
	private static int readOption(List<Integer> validOptions) {
		String line = null;
	    int val = 0;
	    boolean wrongInput = true;
	    while (wrongInput) {
	    	try {
	    		System.out.print("\nOption: ");
	    		BufferedReader is = new BufferedReader(
	    				new InputStreamReader(System.in));
	    		line = is.readLine();
	    		val = Integer.parseInt(line);
	    		for (Integer integer : validOptions) {
	    			if (val == integer) {
	    				wrongInput = false;
	    				break;
	    			}
	    		}
	    	} catch (NumberFormatException ex) {
	    		System.err.println("Not a valid number: " + line);
	    	} catch (IOException e) {
	    		System.err.println("Unexpected IO ERROR: " + e);
	    	}
	    	//System.out.println("I read this number: " + val);
	    }
	    return val;
	}
}
